package com.example.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.video.entity.VideoLog;
import com.example.video.mapper.VideoLogMapper;
import com.example.video.service.IVideoLogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qiusuyang
 * @since 2024-04-13
 */
@Service
public class VideoLogServiceImpl implements IVideoLogService {

    @Resource
    private VideoLogMapper mapper;

    @Override
    public List<VideoLog> getList(Integer page, Integer size){
        QueryWrapper<VideoLog> qwrapper = new QueryWrapper<>();
        IPage<VideoLog> iPage = mapper.selectPage(new Page<>(page,size),qwrapper);
        return iPage.getRecords();
    }

    @Override
    public VideoLog getItem(Integer id){
        return mapper.selectById(id);
    }

    @Override
    public boolean save(VideoLog item){
        if(item.getId() > 0){
            return mapper.updateById(item) > 0;
        }else{
            return mapper.insert(item) > 0;
        }
    }

    @Override
    public boolean delete(Integer id){
        return mapper.deleteById(id) > 0;
    }

    @Override
    public List<VideoLog> getAll() {
        return mapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<VideoLog> getListByVideoId(Integer videoId) {
        return mapper.selectList(new QueryWrapper<VideoLog>().eq("video_id",videoId));
    }

    @Override
    public List<VideoLog> getListByDeviceId(String deviceId) {
        return mapper.selectList(new QueryWrapper<VideoLog>()
                .eq("device_id",deviceId)
                .eq("video_id",0)
        );
    }

    @Override
    public void deleteListByVideoId(Integer videoId) {
        mapper.deleteBatchIds(mapper.selectList(new QueryWrapper<VideoLog>().eq("video_id",videoId)));
    }


}
